package com.iiapk.gridnew.view;

import java.util.List;

import android.content.Context;
import android.support.v1.widget.WrapLayout;
import android.view.View;
import android.widget.LinearLayout;

import com.iiapk.gridnew.domain.Article;

public class NewsPanel extends WrapLayout{
	
	public NewsPanel(Context context,List<Article> dataSource) {
		super(context);
		mOrientation = VERTICAL;
		for(int i=0;i<dataSource.size();i++){
			this.addView(new UnitNews(context,dataSource.get(i),i));
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
      	int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
		final int count = getChildCount();
		int perHeight = parentHeight/(int)Math.ceil(count/2.0);
		int perWidth = parentWidth;
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(perWidth/2, perHeight*2);
		LinearLayout.LayoutParams lpHalf1 = new LinearLayout.LayoutParams(perWidth/2, perHeight);
		int randomSum=5;
		
		for(int i=0;i<count;i++){
			if(i==randomSum){
				View child0 = getChildAt(randomSum);
				if(child0!=null){
					child0.setLayoutParams(lp);
					measureChildWithMargins(child0, widthMeasureSpec, 0, heightMeasureSpec, 0);
				}
				continue;
			}
	       	View child = getChildAt(i);
	       	child.setLayoutParams(lpHalf1);
	       	measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
       	}
		setMeasuredDimension(parentWidth, parentHeight);
	}
	
}
